package com.hfzy.ihk.web.aliyuncall.support.websocket;


import com.alibaba.dubbo.config.annotation.Reference;
import com.hfzy.ihk.facade.websocket.linkcall.service.WebSocketService;
import com.hfzy.ihk.web.aliyuncall.constant.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2016/6/7.
 * @author wws
 *
 */
@Component
public class WebSocketUtils {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketUtils.class);

    @Reference(version = "1.0.0",check = false,retries=0,timeout = 5000)
    private WebSocketService webSocketService;

    @Autowired
    RedisTemplate redisTemplate;


    public  String pushToClientBySessionId(String sessionId, String json){
        String res = null;
        logger.info("user remote web socket push to client");
        try {
            if (webSocketService.pushToBrowserBySessionId(sessionId, json)) {
                res = Constants.SUCCESS;
            } else {
                res = Constants.FAIL;
            }
        }catch (Exception e){
            logger.error("推送websocket 异常,sessionId:{}",sessionId,e);
            res = Constants.FAIL;
        }
        return res;

    }

    public  String pushToClientByToken(String token,String json){

        String res = null;
        logger.debug("user remote web socket push to client");

        if(webSocketService.pushToBrowserByToken(token,json)) {
            res = Constants.SUCCESS;
        } else {
            res = Constants.FAIL;
        }
        return res;

    }



    public  boolean pushToChromeClientByUserId(String userId,String json){
        boolean res = webSocketService.pushToBrowserByUserId4ChromePlugin(userId,json);
        return res;
    }

    public  boolean isFlagWebSocketChromePlugin(String userId){
        return webSocketService.isFlagWebSocketChromePlugin(userId);
    }

    /**
     * 通过用户id发送到app
     * @param userId
     * @param json
     * @return
     */
    public  String pushToAppByUserId(String userId, String json,String isTest){

        String res = null;

        if(webSocketService.pushToAppByUserId(userId,json,isTest))
            res = Constants.SUCCESS;
        else
            res = Constants.FAIL;

        return res;

    }

    /**
     * 通过用户id发送到app
     * @param userId
     * @param toAppjson call任务json
     * @param cacheJson 缓存到app的websocket的
     * @return
     */
    public  String pushToAppByUserId(String userId, String toAppjson,String cacheJson,String isTest){

        String res = null;

        if(webSocketService.pushToAppByUserId(userId,toAppjson,cacheJson,isTest)) {
            res = Constants.SUCCESS;
        } else {
            res = Constants.FAIL;
        }

        return res;

    }

    public  String closeAllAppConnection(){
        return webSocketService.closeAllAppConnetion();
    }
}
